<template>
  <c-dialog ref="dialog" :open-fun="openFun" title="发起异常" @open="openHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="80px">
      <c-list-item prop="exceptionHandleId" label="异常类型">
        <el-select v-model="formData.exceptionHandleId" placeholder="异常类型" clearable>
          <el-option v-for="(e,i) in exceptionList" :key="i" :label="e.exceptionType" :value="e.id" />
        </el-select>
      </c-list-item>
      <c-list-item prop="exceptionCauseType" label="委派异常原因">
        <el-select v-model="formData.exceptionCauseType" placeholder="委派异常原因" clearable>
          <el-option v-for="(e,i) in exceptionTypeList" :key="i" :label="e.dictName" :value="e.dictCode" />
        </el-select>
      </c-list-item>
      <c-list-item prop="exceptionDesc" label="异常说明">
        <el-input v-model="formData.exceptionDesc" type="textarea" maxlength="1024" show-word-limit :autosize="{minRows: 3}" placeholder="异常说明" clearable />
      </c-list-item>
      <c-list-item label="附件上传">
        <c-file-table-box v-model="files" type-code="ACCESS046" :table-height="100" belong-model="SHOP_DECORATION" :model-id="selData&&selData.length>0&&selData[0].id" />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { getDictByPatchType } from '@/api/dataDict'
import { sendException, postExceptionType } from '@/api/decorate'
import { otherFiltersItem } from '@/utils'
export default {
  name: 'Abnormal',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      files: [],
      exceptionList: [],
      exceptionTypeList: [],
      formData: {
        exceptionHandleId: '',
        exceptionCauseType: '',
        exceptionDesc: ''
      },
      formRules: {
        exceptionHandleId: [{ required: true, message: '请选择异常类型', trigger: 'change' }],
        exceptionCauseType: [{ required: true, message: '请选择委派异常原因', trigger: 'change' }],
        exceptionDesc: [{ required: true, message: '请填写异常说明', trigger: 'change' }]
      }
    }
  },
  async created() {
    const { EXCEPTION_CAUSE_TYPE } = await getDictByPatchType(['EXCEPTION_CAUSE_TYPE'])
    this.exceptionTypeList = EXCEPTION_CAUSE_TYPE
  },
  methods: {
    openFun() {
      if (!this.singleJudge(this.selData, '发起异常', true)) {
        return false
      }
      const d = this.selData[0]
      if (d.state === 'D') {
        this.$message.warning('装修状态为异常暂停，不能发起异常！')
        return false
      }
      return true
    },
    openHandle() {
      const data = new FormData()
      data.append('pageFlag', false)
      data.append('filtersRaw', JSON.stringify([
        otherFiltersItem('belongModel', 'SHOP_DECORATION', '='),
        otherFiltersItem('typeCode', 'EXCEPT012', '=')
      ]))
      postExceptionType(data).then(res => {
        this.exceptionList = res.data.records
      })
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const d = this.selData[0]
        const t = this.$gsed()
        t.exceptionDesc = this.formData.exceptionDesc
        // t.belongDuty = d.belongDuty || ''
        t.remark = ''
        const fileList = this.files.map(el => { return el.response.data })
        const data = {
          id: d.id,
          exceptionDTO: {
            shopAttachmentDTOS: fileList,
            bizCode: d.decorationTaskCode,
            constCode: d.constCode,
            exceptionHandleId: this.formData.exceptionHandleId,
            exceptionCauseType: this.formData.exceptionCauseType,
            exceptionDesc: this.formData.exceptionDesc,
            modelId: d.id,
            shopExceptionDetailDTOS: [t]
          }
        }
        return sendException(data).then(res => {
          this.$message.success('操作成功！')
          this.$refs.dialog.visible = false
          this.$emit('on-success')
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    }
  }
}
</script>
